package com.glc.hms.core.interceptor;

import com.glc.hms.common.data.ErrorResponseData;
import com.glc.hms.common.exception.BusinessException;
import com.glc.hms.common.exception.CoreExceptionEnum;
import com.glc.hms.common.exception.ServiceException;
import com.glc.hms.common.util.HttpUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.DisabledAccountException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Santa
 * @since 2019/11/14
 */
@ControllerAdvice
@Order(-1)
public class GlobalExceptionHandler {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 拦截业务异常
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ErrorResponseData service(ServiceException e, HttpServletResponse response) {
        //LogManager.me().executeLog(LogTaskFactory.exceptionLog(ShiroKit.getUser().getId(), e));
        HttpUtil.getRequest().setAttribute("tip", e.getMessage());
        log.error("业务异常:", e);
        log.info(e.getCode()+"===="+e.getMessage());
        response.setStatus(201);
        return new ErrorResponseData(e.getCode(), e.getMessage());
    }

    /**
     * 拦截业务异常
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ErrorResponseData bussiness(ServiceException e, HttpServletResponse response) {
        //LogManager.me().executeLog(LogTaskFactory.exceptionLog(ShiroKit.getUser().getId(), e));
        HttpUtil.getRequest().setAttribute("tip", e.getMessage());
        log.error("错误提示:", e);
        response.setStatus(201);
        return new ErrorResponseData(e.getCode(), e.getMessage());
    }

    /**
     * 用户未登录异常
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ErrorResponseData unAuth(AuthenticationException e) {
        log.error("用户未登陆：", e);
        return new ErrorResponseData(401, "用户未登陆");
    }

    /**
     * 账号被冻结异常
     */
    @ExceptionHandler(DisabledAccountException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ErrorResponseData accountLocked(DisabledAccountException e, Model model) {
        String username = HttpUtil.getRequest().getParameter("username");
        //LogManager.me().executeLog(LogTaskFactory.loginLog(username, "账号被冻结", getIp()));
        model.addAttribute("tips", "账号被冻结");
        return new ErrorResponseData(401, "账号被冻结");
    }

    /**
     * 账号密码错误异常
     */
    @ExceptionHandler(CredentialsException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ErrorResponseData credentials(CredentialsException e, Model model) {
        String username = HttpUtil.getRequest().getParameter("username");
        //LogManager.me().executeLog(LogTaskFactory.loginLog(username, "账号密码错误", getIp()));
        model.addAttribute("tips", "账号密码错误");
        return new ErrorResponseData(500, "账号密码错误");
    }



    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ErrorResponseData notFount(RuntimeException e) {
        //LogManager.me().executeLog(LogTaskFactory.exceptionLog(ShiroKit.getUser().getId(), e));
        HttpUtil.getRequest().setAttribute("tip", "服务器未知运行时异常");
        log.error("运行时异常:", e);
        return new ErrorResponseData(CoreExceptionEnum.SERVER_ERROR.getCode(), CoreExceptionEnum.SERVER_ERROR.getMessage());
    }
}






















